package com.plm.frame.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.plm.frame.service.CartService;
import com.plm.frame.service.ProductService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.plm.frame.core.Constants;
import com.plm.frame.model.Cart;
import com.plm.frame.model.Product;
import com.plm.frame.model.User;

@Controller
public class CartAction extends BaseAction{

	@Resource
	private CartService cartService;
	@Resource
	private ProductService productService;
	
	@RequestMapping("/addCart")
	public String addCart(Map<String,List> m,Product product){
		Cart cart = new Cart();
		cart.setUsername(((User) getSession().getAttribute(Constants.LOGIN_USER)).getUsername());
		cart.setProduct(productService.selectProductAsId(product));
		cartService.addCart(cart);
		m.put("cart", cartService.selectCart(cart));
		return "redirect:cart";
	}
	
	@RequestMapping("/cart")
	public String cart(Map<String,Object> m){
		Cart cart = new Cart();
		cart.setUsername(((User) getSession().getAttribute(Constants.LOGIN_USER)).getUsername());
		m.put("cart", cartService.selectCart(cart));
		if (cartService.selectCart(cart).size() == 0){
			m.put("wucart", "购物车为空");
		}
		return "cart";
	}
	
	@RequestMapping("/deleteCart")
	public String deleteCart(Map<String,List> m,Cart cart){
		cartService.deleteCart(cart);
		return "redirect:cart";
	}
}
